package com.qys.livesMall.financial.controller.app;

import cn.hutool.extra.servlet.ServletUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.app.user.mapper.H2AppUserMapper;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.app.user.utils.PasswordUtil;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.financial.model.dto.app.AppFinancialOnlinePageDTO;
import com.qys.livesMall.financial.model.dto.app.FinancialOnlineContractDTO;
import com.qys.livesMall.financial.model.dto.app.TakeFinancialOnlineDTO;
import com.qys.livesMall.financial.model.entity.FinancialOnlineDetail;
import com.qys.livesMall.financial.model.vo.app.AppFinancialOnlinePageVO;
import com.qys.livesMall.financial.service.FinancialOnlineDetailService;
import com.qys.livesMall.financial.service.FinancialOnlineService;
import com.qys.livesMall.framework.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 线上理财表 前端控制器
 *
 * @author
 * @since 2023-06-13
 */
@RestController
@Tag(name = "APP-线上理财")
@RequestMapping("/app/financial/online")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppFinancialOnlineController {

    private final FinancialOnlineService financialOnlineService;
    private final FinancialOnlineDetailService financialOnlineDetailService;
    private final IH2AppUserService userService;


    @Operation(summary = "分页查询线上理财列表")
    @PostMapping(value = "/financialOnlinePage")
    public R<Page<AppFinancialOnlinePageVO>> financialOnlinePage(@RequestBody AppFinancialOnlinePageDTO pageDTO) {
        pageDTO.setOrderField("limit_user_level");
        pageDTO.setOrderType("asc");
        pageDTO.setStatus((byte) 1);
        Page<AppFinancialOnlinePageVO> result = financialOnlineService.pageByApp(pageDTO);
        if (result.getRecords() != null && CollectionUtils.isNotEmpty(result.getRecords())) {
            List<AppFinancialOnlinePageVO> financials = new ArrayList<>();
            result.getRecords().forEach(e -> {
                LambdaQueryWrapper<FinancialOnlineDetail> wrapper = Wrappers.<FinancialOnlineDetail>lambdaQuery()
                        .eq(FinancialOnlineDetail::getOnlineFinancialId, e.getId());
                //购买人数
                long count = financialOnlineDetailService.count(wrapper);
                e.setBuyCount(count);
                //是否已经购买
                wrapper.eq(FinancialOnlineDetail::getUserId, SecurityUtils.getUserId()).last("limit 1");
                FinancialOnlineDetail one = financialOnlineDetailService.getOne(wrapper);
                e.setBuy(null != one);
                if (null != one) {
                    e.setDividendStatus(one.getStatus());
                    e.setDividendTime(one.getCreateTime().plusDays(one.getPeriod()));
                }
                financials.add(e);
            });
            result.setRecords(financials);
        }
        return R.ok(result);
    }

    @Operation(summary = "参与线上理财")
    @PostMapping(value = "/takePartIn")
    public R<?> takePartIn(@RequestBody @Validated TakeFinancialOnlineDTO takeDTO) {
        financialOnlineService.takePartIn(takeDTO);
        return R.ok();
    }

    @Operation(summary = "获取投资合同")
    @PostMapping(value = "/getContract")
    public R<?> getContract(@RequestBody @Validated FinancialOnlineContractDTO contract) {
        return R.ok(financialOnlineDetailService.buildContract(contract));
    }


}
